package Ep01_Array.Feb25th_0013;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jimmy Zhan WORKSTATION
 * @Date 2023/2/27 10:38
 * 说明：13 罗马数字转整数
 */

public class Best {
    public static void main(String[] args) {
        Best b = new Best();
        String s = "III";
        System.out.println(b.romanToInt(s));
    }

    Map<Character, Integer> symbolValues = new HashMap<Character, Integer>(){{
        put('I', 1);
        put('V', 1);
        put('X', 1);
        put('L', 1);
        put('C', 1);
        put('D', 1);
        put('M', 1);
        }};
    public int romanToInt(String s){
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int value = symbolValues.get(s.charAt(i));
            if (i < n-1 && value < symbolValues.get(s.charAt(i+1))){
                ans -= value;
            } else {
                ans += value;
            }
        }
        return ans;

    }
}
